from tkinter import *
from tkinter import messagebox
from db_operations import DBOperations

class OutboundUI:
    def __init__(self, root):
        self.root = root
        self.db = DBOperations()
        
        self.frame = Frame(self.root, padx=20, pady=20)
        self.frame.pack(expand=True)
        
        Label(self.frame, text="SN码:").grid(row=0, column=0, sticky=W, pady=5)
        self.sn_entry = Entry(self.frame)
        self.sn_entry.grid(row=0, column=1, pady=5)
        Button(self.frame, text="扫码", command=self.scan_sn).grid(row=0, column=2, padx=5)
        
        Button(self.frame, text="确认出库", command=self.confirm_outbound).grid(row=1, column=1, pady=20)
    
    def scan_sn(self):
        # 模拟扫码功能
        self.sn_entry.delete(0, END)
        self.sn_entry.insert(0, "SN" + str(datetime.now().timestamp()).replace(".", "")[:10])
    
    def confirm_outbound(self):
        sn = self.sn_entry.get()
        if not sn:
            messagebox.showerror("错误", "请输入SN码")
            return
            
        if self.db.outbound_equipment(sn):
            messagebox.showinfo("成功", "设备出库成功")
            self.sn_entry.delete(0, END)
        else:
            messagebox.showerror("错误", "设备不存在或已出库")